#!/bin/bash
db_connect() {
    mysql -hcm-mysql -uroot -p123.com cm-shell -e "$1"

}

getData() {
    Callback=$1
    sql=$2
    file=$(mktemp /tmp/show.XXXXXXXXXX)
    db_connect "$sql" | sed -e '1d' >$file
    while read x; do
        $Callback $x
    done <$file
    rm $file
}

getVersion() {
    Callback=$1
    file=$(mktemp /tmp/show.XXXXXXXXXX)
    for project in $(ls -l task | awk '{print $9}'); do
        ls -l task/$project/package/ | awk '{print $8,$9}' | grep -v "^\s" | awk -v p=$project '{print p,$1,$2}' | sort -r >>$file
    done
    j=0
    while read x; do
        $Callback $j $x

        j=$((j + 1))
    done <$file
    rm $file

}

DataHanle() {
    db_connect "$sql"
    if [ $? != 0 ]; then
        logInput Write " 操作失败  $sql"
        echo "操作失败 $sql"
    else
        echo "操作成功"
        echo "<script>window.location.href=\"index.cgi\";</script>"

    fi

}

TaskHanle() {
    db_connect "$sql"
    if [ $? != 0 ]; then
        logInput Write " 操作失败  $sql"
        echo "操作失败 $sql"
    else
        echo "<script>window.location.href=\"form.cgi?action=show_task_result&task=$file\"</script>"

    fi

}

deleteDb() {
    table=$1
    Id=$2
    sql="DELETE FROM $table WHERE id=$Id"
    db_connect "$sql"
    if [ $? != 0 ]; then
        logInput Write "  删除数据失败  $sql"
        echo "删除数据失败"
    else
        echo "删除数据成功"
        echo "<script>window.location.href=\"index.cgi\";</script>"

    fi

}
